/**
 * Created by 白雨浓 on 2018/1/17 22:03.
 */

import electron from 'electron';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';

export default{
    controller(self, name){

        const dataPath = (electron.app || electron.remote.app).getPath('userData');
        const filePath = path.join(dataPath, 'controller.ejs');
        if (!fs.existsSync(filePath)) {
            self.$$msg.error('controller模板文件缺失');
            return;
        }
        const tmpFile = fs.readFileSync(filePath, 'utf-8');

        let clsName = self.$str.toCamel(name);
        clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

        const url = '/' + self.$str.toUnderline(clsName).replace(/_/g, '/');

        let tmp = {
            packageName: self.config.packageName,
            author: self.config.author,
            date: self.$date.format(new Date(), 'yyyy/MM/dd hh:mm'),
            clsName: clsName,
            url: url,
        };

        return ejs.render(tmpFile, tmp);
    }
}